package com.example.springbootrbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.repository.*;
import com.example.springbootrbac.service.QueryService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class QueryServiceImpl implements QueryService {
    @Resource(name="announcementRepository")
    AnnouncementRepository announcementRepository;
    @Resource(name="roomRepository")
    RoomRepository roomRepository;
    @Resource(name="courseRepository")
    CourseRepository courseRepository;
    @Resource(name="messagesBoardRepository")
    MessagesBoardRepository messagesBoardRepository;
    @Resource(name = "userRepository")
    UserRepository userRepository;

    @Override
    public List<AnnouncementEntity> queryAnnouncement(String title,String createBy) {
        List<AnnouncementEntity> announcementEntityList = announcementRepository.selectList(
                       new QueryWrapper<AnnouncementEntity>()
                               .select("id","title","text","create_by","create_time")
                               .eq("create_By",createBy)
                               .like("title",title)
                               .or()
                               .likeRight("title",title)
                               .eq("create_By",createBy)
                               .orderByDesc("create_time")
        );
        if(!ObjectUtils.isEmpty(announcementEntityList)){
            return announcementEntityList;
        }
        return null;
    }

    @Override
    public List<UserEntity> queryUser(String username, String status , HttpServletRequest request) {
        HttpSession session =request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");

        if ("DBA".equals(user.getIdentity())){
            List<UserEntity> userEntityList = userRepository.selectList(
                    new QueryWrapper<UserEntity>()
                            .select("id","username","status")
                            .like("status",status)
                            .like("username",username)
                            .or()
                            .likeRight("username",username)
                            .likeRight("status",status)
                            .orderByDesc("create_time")
            );

            if(!ObjectUtils.isEmpty(userEntityList)){
                return userEntityList;
            }
        }
        return null;
    }

    @Override
    public List<RoomEntity> queryRoom(String name,String quality,int available) {
        List<RoomEntity> roomEntityList = roomRepository.selectList(
                new QueryWrapper<RoomEntity>()
                        .select("id","name","belong","household","quality","occupied","available")
                        .like("quality",quality)
                        .ge("available",available)
                        .like("name",name)
                        .or()
                        .likeRight("quality",quality)
                        .ge("available",available)
                        .likeRight("name",name)
                        .orderByDesc("create_time")
        );
        if(!ObjectUtils.isEmpty(roomEntityList)){
            return roomEntityList;
        }
        return null;
    }

    @Override
    public List<MessagesBoardEntity> queryMessagesBoard(String text,String createBy) {
        List<MessagesBoardEntity> messagesBoardEntityList = messagesBoardRepository.selectList(
                new QueryWrapper<MessagesBoardEntity>()
                        .select("id","text","create_by","create_time")
                        .eq("create_by",createBy)
                        .like("text",text)
                        .or()
                        .eq("create_by",createBy)
                        .likeRight("text",text)
                        .orderByDesc("create_time")
        );
        if(!ObjectUtils.isEmpty(messagesBoardEntityList)){
            return messagesBoardEntityList;
        }
        return null;
    }

    @Override
    public List<CourseEntity> queryCourse(String courseSql, String teacherSql) {
        List<CourseEntity> courseEntityList = null;
        if(ObjectUtils.isEmpty(courseSql) && !ObjectUtils.isEmpty(teacherSql)){   //按教师查询
            courseEntityList = courseRepository.selectList(
                    new QueryWrapper<CourseEntity>()
                            .select("id","name","teacher","address","week","day","period","length")
                            .like("teacher",teacherSql)
                            .orderByDesc("create_time")
            );
        }else if(ObjectUtils.isEmpty(teacherSql) && !ObjectUtils.isEmpty(courseSql)){  //按课程查询
            courseEntityList = courseRepository.selectList(
                    new QueryWrapper<CourseEntity>()
                            .select("id","name","teacher","address","week","day","period","length")
                            .like("name",courseSql)
                            .orderByDesc("create_time")
            );
        }else {
            courseEntityList = courseRepository.selectList(      //两种同时使用
                    new QueryWrapper<CourseEntity>()
                            .select("id","name","teacher","address","week","day","period","length")
                            .like("name",courseSql)
                            .like("teacher",teacherSql)
                            .orderByDesc("create_time")
            );
        }

        if(!ObjectUtils.isEmpty(courseEntityList)){
            return courseEntityList;
        }
        return null;
    }
}
